package com.fesine.quartz.chapter4;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.InterruptableJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.UnableToInterruptJobException;

/**
 * @description: 中断job范例
 * @author: Fesine
 * @createTime:2016/11/18 20:08
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/18 20:08
 */
public class CheckForInterruptJob implements InterruptableJob {
    static Log logger = LogFactory.getLog(CheckForInterruptJob.class);

    private boolean jobInterrupted = false;
    private int counter = 5;
    private boolean jobFinished = false;
    @Override
    public void interrupt() throws UnableToInterruptJobException {
        jobInterrupted = true;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        while (!jobInterrupted && !jobFinished) {
            logger.info("job processing...");
            counter--;
            if (counter <= 0) {
                jobFinished = true;
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (jobFinished) {
            logger.info("job finished without interrupt");
        }else if (jobInterrupted) {
            logger.info("job was interrupted");
        }
    }
}
